package com.bocloud.cmp.driver.providers.impl;

import org.springframework.stereotype.Service;

import com.bocloud.cmp.driver.config.Configuration;
import com.bocloud.cmp.driver.providers.DatabaseDriver;
import com.bocloud.cmp.driver.providers.ali.AliRdsDriver;
import com.bocloud.cmp.driver.providers.intf.DatabaseProvider;
import com.bocloud.cmp.driver.providers.jd.JdDatabaseDriver;
import com.bocloud.cmp.driver.utils.VendorConverter;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.cmp.model.Butler;
import com.bocloud.cmp.model.DbInstanceModel;
import com.bocloud.common.model.BsmResult;

@Service("databaseProvider")
public class DatabaseProviderImpl implements DatabaseProvider {

	@Override
	public BsmResult create(String vendor, String region, DbInstanceModel database) {
		BsmResult result = null;
		DatabaseDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdDatabaseDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.createInstance(database);
			break;
		case ALIYUN:
			driver = new AliRdsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.createInstance(database);
			break;
		default:
			result = new BsmResult(false, "暂不支持");
			break;
		}
		return result;
	}

	@Override
	public BsmResult detail(String instanceId, String vendor, String region) {
		BsmResult result = null;
		DatabaseDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdDatabaseDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.instanceDetail(instanceId);
			break;
		case ALIYUN:
			driver = new AliRdsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.instanceDetail(instanceId);
			break;
		default:
			result = new BsmResult(false, "暂不支持");
			break;
		}
		return result;
	}

	@Override
	public BsmResult remove(String instanceId, String vendor, String region) {
		BsmResult result = null;
		DatabaseDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdDatabaseDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.removeInstance(instanceId);
			break;
		case ALIYUN:
			driver = new AliRdsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.removeInstance(instanceId);
			break;
		default:
			result = new BsmResult(false, "暂不支持");
			break;
		}
		return result;
	}

	@Override
	public BsmResult list(String vendor, String region) {
		DatabaseDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdDatabaseDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			break;
		case ALIYUN:
			driver = new AliRdsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			return driver.listInstances();
		}
	}

	@Override
	public BsmResult reboot(String instanceId, String vendor, String region) {
		BsmResult result = null;
		DatabaseDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdDatabaseDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.rebootInstance(instanceId);
			break;
		case ALIYUN:
			driver = new AliRdsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.rebootInstance(instanceId);
			break;
		default:
			result = new BsmResult(false, "暂不支持");
			break;
		}
		return result;
	}

}
